﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace SP.KeywordsTranslator
{
    [ToolboxItemAttribute(false)]
    public class WPKeywordsTranslator : WebPart
    {
        #region Variables

        private TextBox AddKeyword;
        private TextBox AddvalueEN;
        private TextBox AddvalueFR;
        private CheckBox CheckIsGlobal;
        private Button Ctr_Submit;
        private SPLanguageCollection installedLanguages;
        private int lcid = 0;
        private int maxRows = 2000;
        private Literal message;

        public int LCID
        {
            get
            {
                if (lcid == 0)
                {
                    lcid = CultureInfo.CurrentUICulture.LCID;
                }
                return lcid;
            }
        }

        #endregion Variables

        protected override void CreateChildControls()
        {
            base.EnsureChildControls();
            installedLanguages = SPRegionalSettings.GlobalInstalledLanguages;

            //Register JQuery script to be used within the SharePoint Web Part
            Utility.RegisterJSScript(Constantes.JQUERY_PATH, this);
            Utility.RegisterJSScript(Constantes.JS_PATH_LANGUAGE, this);

            if (IsEditMode())
            {
                Ctr_Submit = new Button();
                Ctr_Submit.ID = "Add";
                Ctr_Submit.UseSubmitBehavior = false;
                Ctr_Submit.Text = Utility.GetLocalizedString("ctr_Sudmit_Text", LCID);
                Ctr_Submit.OnClientClick = "AddDictionaryItem(this.form)";

                message = new Literal();
                AddKeyword = new TextBox();
                AddKeyword.ID = Constantes.ADD_KEYWORD;

                CheckIsGlobal = new CheckBox();
                CheckIsGlobal.ID = "global";
                CheckIsGlobal.LabelAttributes.Add("Global", "true");
                CheckIsGlobal.Text = "Global";

                foreach (SPLanguage item in installedLanguages)
                {
                    TextBox t = new TextBox();
                    t.ID = item.LCID.ToString();

                    this.Controls.Add(t);
                }

                this.Controls.Add(message);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            Type cstype = this.GetType();

            string dataJs = " '" +
                Utility.GetLocalizedString("H1_DISPLAY_DICTIONARY_TEXT", LCID) + "', " +
                " '" + Constantes.TRANSLATE_LISTNAME + "', " +
                " '" + Utility.GetLocalizedString("ERROR_MESSAGE_JS", LCID) + "', " +
                " '" + Utility.GetLocalizedString("ERROR_MESSAGE_JS_CANCEL", LCID) + "', " +
                " '" + Utility.GetLocalizedString("LIST_TEXT_JS_CREATED", LCID) + "', " +
                " '" + Utility.GetLocalizedString("LIST_TEXT_JS", LCID) + "', " +
                " '" + maxRows + "' ,'" + Utility.GetLocalizedString("ERROR_MESSAGE_JS_SAMEKEYWORD", LCID) + "'";

            if (IsBrowse())
            {
                string passScript = "ExecuteOrDelayUntilScriptLoaded(function() {setData(" + dataJs + "); setLanguages(" + GetJsLanguages() + "); RetrieveListItems(true);  }, \"sp.js\")";
                Page.ClientScript.RegisterClientScriptBlock(cstype, Constantes.csName, passScript, true);
            }
            else
            {
                string passScript = "$(function() {setData(" + dataJs + "); RetrieveListItems(false); setLanguages(" + GetJsLanguages() + "); });";

                // string passScript = "ExecuteOrDelayUntilScriptLoaded(function() {setData(" + dataJs + "); RetrieveListItems(false);  }, \"Language.js\")";
                Page.ClientScript.RegisterClientScriptBlock(cstype, Constantes.csName, passScript, true);
                string passScript2 = "ExecuteOrDelayUntilScriptLoaded(function() {RetrieveListItems(false); }, \"sp.js\")";
                Page.ClientScript.RegisterClientScriptBlock(cstype, Constantes.csName, passScript2, true);
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (IsEditMode())
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, Constantes.TRANSLATE_LISTNAME);
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.H1);
                writer.Write(Utility.GetLocalizedString("H1_Instruction_TEXT", LCID));
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.Table);
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(Utility.GetLocalizedString("KEYWORD_TEXT", LCID));
                writer.RenderEndTag();
                foreach (SPLanguage item in installedLanguages)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(item.DisplayName + " ( " + item.LCID.ToString() + " )");
                    writer.RenderEndTag();
                }

                writer.RenderBeginTag(HtmlTextWriterTag.Td);

                writer.RenderEndTag();
                writer.RenderEndTag();//fin TR

                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                AddKeyword.RenderControl(writer);
                writer.RenderEndTag();

                foreach (SPLanguage item in installedLanguages)
                {
                    TextBox tb;

                    //Find the TextBox Control named "tbTest"
                    tb = (TextBox)GetControlByName(item.LCID.ToString());
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    tb.RenderControl(writer);
                    writer.RenderEndTag();
                }

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                CheckIsGlobal.RenderControl(writer);
                writer.RenderEndTag();
                writer.RenderEndTag(); //fin TR

                writer.RenderEndTag(); // fin Table
                Ctr_Submit.RenderControl(writer);
            }
        }

        private Control GetControlByName(string Name)
        {
            foreach (Control c in this.Controls)
                if (c.ID == Name)
                    return c;
            return null;
        }

        private string GetJsLanguages()
        {
            StringBuilder setLanguages = new StringBuilder();
            setLanguages.Append("new Array(");
            for (int i = 0; i < installedLanguages.Count; i++)
            {
                SPLanguage language = installedLanguages[i];
                setLanguages.Append(@"'" + language.LCID.ToString() + "'");
                if (i < installedLanguages.Count - 1)
                { setLanguages.Append(@","); }
            }
            setLanguages.Append(")");
            return setLanguages.ToString();
        }

        private bool IsBrowse()
        {
            WebPartManager wp = WebPartManager.GetCurrentWebPartManager(Page);
            return wp.DisplayMode.Name == WebPartManager.BrowseDisplayMode.Name;
        }

        private bool IsEditMode()
        {
            WebPartManager wp = WebPartManager.GetCurrentWebPartManager(Page);

            return wp.DisplayMode.Name == WebPartManager.EditDisplayMode.Name;
        }
    }
}